#include <stdio.h>
#include<stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include<stdint.h>
#include <iconv.h>
#include <ctype.h>
#include <pthread.h>

 #include<math.h>

void main()
{
    int fd = open("/home/wen/1.txt",O_RDWR | O_CREAT ,0777);//第二次打开才会覆盖掉原文件内容，可用lseek设置读写的位置
    if (-1 == fd)
    {
        printf("creat or open file error\r\n");
        return fd;
    }
    //要写入的数据
    char *p = NULL;        
    char *q = "12345";
    char *f = "999999999";
    printf("%s\n",q);

    //写数据
    p = q;
    write(fd,p,strlen(p));
    //将光标位置移到文件结尾，第二次打开文件写入时不会覆盖文件原内容
    off_t off = lseek(fd, 0, SEEK_END);
    if (-1 == off)
     return -1;
    //将光标位置移到文件开头
    off_t off = lseek(fd, 0, SEEK_SET);
    if (-1 == off)
    return -1;
    //将读写位置移动到偏移文件开头 10 个字节处
    off_t off = lseek(fd, 10, SEEK_SET);
    if (-1 == off)
    return -1;
    //获取当前读写位置偏移量
    off_t off = lseek(fd, 0, SEEK_CUR);
    if (-1 == off)
    return -1;


    p = f;
    int flag = write(fd,p,strlen(p));//返回成功写入的字符数，失败返回-1
    if(flag ==-1)
    {
        printf("write error\r\n");
    }
    flag = read(fd,p,strlen(p));//返回成功读到的字符数，受lseek影响，失败返回-1
    if(flag ==-1)
    {
        printf("read error\r\n");
    }
    close(fd);
}